{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:game type='manage' /}
        </div>
    </div>
    <div class="layui-inline" style="width:293px;">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
        <button class="layui-btn my-btn my-btn-sm" type="button" id="add">添加素材</button>
        <button class="layui-btn my-btn my-btn-sm" type="button" id="vip">添加游戏VIP表</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="tableBar">
    <a href="{{ d.url }}" class="layui-btn layui-btn-green layui-btn-sm">下载</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var game_id = $('#game_id').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: {game_id:game_id}
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("ChannelMaterial/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function () {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'game_id', title: '游戏ID', width: 100}
                ,{field:'game_name', title: '游戏名称', width: 200}
                ,{field:'url', title: '下载地址', width: 600}
                ,{field:'add_time', title: '添加时间', width: 180, sort:true}
                ,{field:'status', title: '状态', width: 120, sort:true, templet: '#switchTpl', unresize: true}
                ,{title: '操作', align:'center', width: 200, toolbar:'#tableBar'}
            ]]
        });

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        //修改状态
        form.on('switch(status)', function(obj){
            var status = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            $.post('{:url("ChannelMaterial/edit")}',{id:obj.value, status:status}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });

        //监听搜索栏
        $('#add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['100%', '100%']
                ,content: '{:url("ChannelMaterial/add")}'
            });
        })
        $('#vip').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['100%', '100%']
                ,content: '{:url("ChannelMaterial/vip")}'
            });
        })


        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data;
            console.dir(data);
            if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("ChannelMaterial/del")}',{id: data.id, file_name:data.file_name}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }
        });
    });
</script>
{/block}